package com.kalua.list.harvester.parser;

import static com.kalua.list.harvester.parser.ProductIdGenerationUtils.generateId;

import com.kalua.list.domainmodel.NumericId;
import com.kalua.list.domainmodel.TextIdentifier;

public abstract class AbstractProductParseFunction implements ProductParseFunction {
	
	private NumericId batchId;
	
	protected void setBatchId(NumericId batchId) { this.batchId = batchId; }
	protected NumericId getBatchId() { return this.batchId; }
	
	protected NumericId createProductId(	TextIdentifier retailerIssuedId,
																String name) {
		NumericId result;
		TextIdentifier toUse;
		if (retailerIssuedId.isEmpty())
			toUse = new TextIdentifier(name);
		else
			toUse = retailerIssuedId;
		result = generateId(getRetailerId(), toUse);
		return result;
	}
	
	protected String getProductIdAfterEqualsFromLink(String productURL) {
		String result;
		int breakIndex = productURL.indexOf("=");
		int productIdStartIndex = breakIndex+1;
		if (breakIndex>=0 && productIdStartIndex<productURL.length()) {
			result = productURL.substring(productIdStartIndex);
		} else result = ProductParseResult.UNKNOWN_VENDOR_PRODUCT_ID;
		return result;
	}	
}